#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <stdlib.h>
#include <time.h>

//获取当前目录
void mypwd(){
	char pwd[100];
	getcwd(pwd,sizeof(pwd));
    printf("$ %s\n",pwd);
}

//创建目录
void mymkdir(char *path){
	if(mkdir(path,0777) == -1){
		perror("Error: \n");
	}else{
		printf("mkdir %s\n",path);
	}
}

//删除目录
void myrmdir(char *path){
	if(rmdir(path) == -1 ){
		perror("Error: \n");
	}else{
		printf("delete %s\n",path);
	}
}

//切换目录
void mycd(char *path){  
    if (chdir(path) == 0) {  
        printf("Changed directory to %s\n", path);  
    } else {  
        perror("chdir");  
    }  
}

//读取目录
void mylist(char *sdir){  
 	DIR *dir;  
    struct dirent *entry;  
  
    // 打开当前目录  
    if(sdir == NULL){
    	dir = opendir(".");
    }else{
    	dir = opendir(sdir);
    	
    }
      
    if (dir == NULL) {  
        perror("opendir");  
        return ;  
    }  
  
    // 读取目录中的文件和子目录列表  
    while ((entry = readdir(dir)) != NULL) {  
    	if(strcmp(entry->d_name,".") == 0)continue;
    	if(strcmp(entry->d_name,"..") == 0)continue;
        printf("%s\t", entry->d_name);  
    }  
  
    // 关闭目录  
    closedir(dir);  
    printf("\n");
}

//复制文件
void mycp(const char* srcFile, const char* dstFile) {  
    FILE* src = fopen(srcFile, "rb");  
    if (src == NULL) {  
        perror("无法打开源文件"); 
        printf("%s\n",srcFile);
        return;  
    }  
    FILE* dst = fopen(dstFile, "wb");  
    if (dst == NULL) {  
        perror("无法打开目标文件");  
        printf("%s\n",dstFile);
        fclose(src);  
        return;  
    }  
    char buffer[4096];  
    size_t size;  
    while ((size = fread(buffer, 1, sizeof(buffer), src)) > 0) {  
        fwrite(buffer, 1, size, dst);  
    }  
    fclose(src);  
    fclose(dst);  
    printf("%s\t ------->\t%s\n",srcFile,dstFile);
}  

//显示时间
void mydate(){
	time_t current_time;  
    char* c_time_string;  
  
    current_time = time(NULL);  
    c_time_string = ctime(&current_time);  
  
    printf("当前时间: %s", c_time_string); 
}

//创建文件
void mycreate(char * filename,char *str){
	FILE *fp;  
    //char str[] = "Hello, world!";  
  	
    fp = fopen(filename, "w"); // 打开文件以写入  
    if (fp == NULL) {  
        printf("无法打开文件\n");  
        return ;  
    }  
  	if(str != NULL){
  		fprintf(fp, "%s\n", str); // 向文件中写入一行内容  
  	}
    fclose(fp); // 关闭文件  
    printf("文件%s创建成功\n",filename);  
}

//删除文件
void mydelete(char * filename){
    if (remove(filename) == 0) {  
        printf("文件%s已成功删除。\n",filename);  
    } else {  
        printf("无法删除%s文件。\n",filename);  
    }  
}

//移动文件
void mymv(char *srcfile,char *dstfile){
	FILE *source_file, *target_file;  
 
    char buffer[1024];  
    size_t bytes_read;  
  
    // 打开源文件和目标文件  
    source_file = fopen(srcfile, "rb");  
    if (source_file == NULL) {  
        perror("无法打开源文件");  
        return; 
    }  
    target_file = fopen(dstfile, "wb");  
    if (target_file == NULL) {  
        perror("无法打开目标文件");  
        fclose(source_file);  
        exit(EXIT_FAILURE);  
    }  
  
    // 读取源文件内容并写入目标文件  
    while ((bytes_read = fread(buffer, 1, sizeof(buffer), source_file)) > 0) {  
        fwrite(buffer, 1, bytes_read, target_file);  
    }  
  
    // 关闭文件并删除源文件  
    fclose(source_file);  
    fclose(target_file);  
    remove(srcfile);  
    printf("%s\t ------> \t %s\n",srcfile,dstfile);  

}

/**
 * 递归删除目录
 * 参数 dir_path 要删除的目录
 * 参数argstr 参数 -r
 * 
 **/
void myrm(const char *dir_path, char *argstr) {  
    char path[1024];  
    struct dirent *entry;  
    DIR *dir = opendir(dir_path);  
    if (dir == NULL) {  
        perror("无法打开目录");  
        printf("%s",dir_path);
        return ;
    }  
    while ((entry = readdir(dir)) != NULL) {  
        if (entry->d_type == DT_DIR) {  
            if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {  
                continue; // 跳过当前目录和上级目录  
            }  
            if (strcmp(argstr,"-r\n") == 0) { // 如果参数 argstr 不为-r，递归删除子目录  
                snprintf(path, sizeof(path), "%s/%s", dir_path, entry->d_name);  
                myrm(path, argstr);  
            }  
        } else {  
            snprintf(path, sizeof(path), "%s/%s", dir_path, entry->d_name);  
            remove(path); // 删除文件  
            printf("删除文件 %s\n",path);
        }  
    }  
    closedir(dir);  
    if (strcmp(argstr,"-r\n") == 0) { // 如果参数arg不为r，删除空目录  
        rmdir(dir_path);  
        printf("删除目录 	 %s\n",dir_path);
    }  
}

//查找文件
void myfind(const char *dir_path, const char *file_name,char *url) {  
    struct dirent *entry;  
    DIR *dir = opendir(dir_path);  
    if (dir == NULL) {  
        perror("无法打开目录");  
        return ;
    }  
    while ((entry = readdir(dir)) != NULL) {  
        char path[1024];  
        snprintf(path, sizeof(path), "%s/%s", dir_path, entry->d_name);  
        struct stat file_info;  
        if (stat(path, &file_info) == 0) {  
            if (S_ISREG(file_info.st_mode) && strcmp(entry->d_name, file_name) == 0) {  
                printf("%s/%s\n",url, path); 
            } else if (S_ISDIR(file_info.st_mode) && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {  
                myfind(path, file_name,url); // 递归搜索子目录  
            }  
        }  
    }  
    closedir(dir);  
}  
int main(){
	printf("欢迎使用基础命令\n");
	char str[1000];
	char *str1,*str2,*str3;
	for(;;){
		printf("$ ");
		fgets(str, sizeof(str), stdin);  
		str1=strtok(str," ");
		if(strcmp(str1,"mypwd\n") == 0){
			mypwd();
		}
		if(strcmp(str1,"mymkdir") == 0){
			str2=strtok(NULL,"\n");
			mymkdir(str2);
		}
		if(strcmp(str1,"mycd") == 0){
			str2=strtok(NULL,"\n");
			mycd(str2);
		}
		if(strcmp(str1,"mylist") == 0 || strcmp(str1,"mylist\n") == 0){
			str2=strtok(NULL,"\n");
			mylist(str2);
		}
		if(strcmp(str1,"mycp") == 0){
			char *f[100];
			int i = 0;
			char dstr[1000];
			char dstpath[1000];
			char dstfile[1000];
			strcpy(dstr, strtok(NULL,"\n"));
			f[i++] =strtok(dstr," ");
			while(f[i] = strtok(NULL," ")){
				i++;
			}

			memset(dstpath,0,1000);
			strcat(dstpath, f[--i]);
			if(i>1){
				//printf("file to dir\n");
				int j = 0;
				while(f[j] != NULL){
					strcpy(dstfile, dstpath);
					strcat(dstfile, "/");
					strcat(dstfile, f[j]);
					mycp(f[j],dstfile);
					//printf("%s \t------->\t %s\n",f[j],dstfile);
					f[j++];
					if(j==i)break;
				}
			}else{
				printf("file to file\n");
				mycp(f[0],f[1]);
			}
			
		}

		if(strcmp(str1,"mydate\n") == 0){
			str2=strtok(NULL,"\n");
			mydate(str2);
		}

		if(strcmp(str1,"mycreate") == 0 || strcmp(str1,"mycreate\n") == 0){
			int i = 0;
			char *file_arg[100],*filename;
			while(file_arg[i] = strtok(NULL," ")){
				i++;
			}
			if(i<1){
				file_arg[1] = NULL;
			}
			filename = strtok(file_arg[0],"\n");
			mycreate(filename,file_arg[1]);
		}
		
		if(strcmp(str1,"mydelete") == 0){
			str2=strtok(NULL,"\n");
			mydelete(str2);
		}

		if(strcmp(str1,"mymv") == 0 || strcmp(str1,"myrename") == 0){
			int i = 0;
			char *file_arg[100];
			while(file_arg[i] = strtok(NULL," ")){
				i++;
			}
			mymv(file_arg[0],file_arg[1]);
		}

		if(strcmp(str1,"myrm") == 0){
			int i = 0;
			char *file_arg[100],*arg1;
			char arg2[]="-n";
			while(file_arg[i] = strtok(NULL," ")){
				i++;
			}
			if(i<2){
				arg1 = strtok(file_arg[0],"\n");
				myrm(arg1,arg2);
			}else{
				myrm(file_arg[0],file_arg[1]);
			}
			
		}
		if(strcmp(str1,"myfind") == 0){
			int i = 0;
			char *file_arg[100],*arg2;
			while(file_arg[i] = strtok(NULL," ")){
				i++;
			}
			char url[500];
			arg2 = strtok(file_arg[1],"\n");
			char home_str[100];
			getcwd(home_str,sizeof(home_str));
			myfind(file_arg[0],arg2,home_str);
		}


		if(strcmp(str1,"exit\n") == 0){
			break;
		}


	}
	return 0;
}
